﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using OA.Model;
using OA.DBUtility;

namespace OA.Manager
{
    /// <summary>
    /// 数据访问类:Dictionary
    /// </summary>
    public partial class DictionaryManager
    {
        private SqlService<DictionaryInfo> _sqlService = null;

        public DictionaryManager()
        {
            _sqlService = new SqlService<DictionaryInfo>();
        }

        #region  Method

        /// <summary>
        /// 插入新记录
        /// </summary>
        /// <param name="dictionaryInfo"></param>
        /// <returns></returns>
        public bool InsertDictionaryInfo(DictionaryInfo dictionaryInfo)
        {
            return _sqlService.SaveEntity(dictionaryInfo);
        }

        /// <summary>
        /// 根据打印任务ID获得打印任务信息
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public DictionaryInfo GetDictionaryInfoByID(int ID)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Id", ID);
            return _sqlService.GetSingleByFields(dic, null);
        }

        public List<DictionaryInfo> GetDictionaryInfoAll(string sql)
        {

            return _sqlService.GetAllByCondition(sql, "OrderId", -1);
        }
        public List<DictionaryInfo> GetDictionaryInfoAll(int typeid, int parentId)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("typeId", typeid);
            dic.Add("parentId", parentId);
            return _sqlService.GetAllByFields(dic, "OrderId", -1);
        }

        public DataTable GetPaged(int pageIndex, int pageSize, ref int total)
        {
            return _sqlService.GetPaged("*", "", pageIndex, pageSize, ref pageSize, ref total);
            //return _sqlService.GetPaged(pageIndex, pageSize, out total);
        }
        public int GetDictionaryTypeTotal()
        {
            return Convert.ToInt32(_sqlService.ExecuteScalar("select count(*) from Dictionary"));
        }

        public DataTable GetSearchPaged(DictionaryInfo info, int pageIndex, int pageSize, ref int total)
        {
            //string sql = "select top(" + pageSize + ")* from Dictionary where Id not in (select top(" +
            //             (pageSize * pageIndex) + ")Id from Dictionary ) ";
            //string where = SqlUtility.GetSearchWhereStr<DictionaryInfo>(info);
            //sql += where;
            //return _sqlService.GetAllBySqlString(sql);
            //_sqlService.GetPaged("", "", 1, 100, ref 10, ref 20);
            // DataTable dt = _sqlService.GetPaged("*",)

            return _sqlService.GetSearchPaged(info, pageIndex, pageSize, ref total);
        }

        /// <summary>
        /// 判断该打印任务是否存在
        /// </summary>
        /// <param name="dictionaryInfo"></param>
        /// <returns></returns>
        public bool IsExistDictionaryInfo(DictionaryInfo dictionaryInfo)
        {
            return _sqlService.IsExist(dictionaryInfo);
        }

        /// <summary>
        /// 根据指定的字段更新，字段名是数据库中对应的名称
        /// </summary>
        /// <param name="dictionaryInfo"></param>
        /// <param name="needUpdateFields"></param>
        /// <returns></returns>
        public bool UpdateDictionaryInfo(DictionaryInfo dictionaryInfo, List<string> needUpdateFields)
        {
            return _sqlService.UpdateByFields(dictionaryInfo, needUpdateFields);
        }

        /// <summary>
        /// 删除该检查任务
        /// </summary>
        /// <param name="dictionaryInfo"></param>
        /// <returns></returns>
        public bool DeleteDictionaryInfo(DictionaryInfo dictionaryInfo)
        {
            return _sqlService.DeleteEntity(dictionaryInfo);
        }

        #endregion  Method
    }
}

